package com.example.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.constant.HttpConstant;
import com.example.domain.DianYing;
import com.example.domain.Food;
import com.example.domain.GetInformation;
import com.example.domain.HuoDong;
import com.example.domain.JiaoShi;
import com.example.domain.ShouCe;
import com.example.domain.XinWen;
import com.example.service.InfoService;
import com.example.util.HttpUtil;
import com.example.util.HttpUtil2;

public class InfoServiceImpl implements InfoService {

	@Override
	public List<XinWen> getXinWens() {
		List<XinWen> list = new ArrayList<XinWen>();
		String servlet = "InfoServlet";
		String method = "getXinWens";
		String result = HttpUtil.sendHttp(servlet, method, null);
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getXinWens");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					XinWen xinWen = new XinWen();
					xinWen.setXw_bt(object.getString("xw_bt"));
					xinWen.setXw_ly(object.getString("xw_ly"));
					xinWen.setXw_sj(object.getString("xw_sj"));
					xinWen.setXw_nr(object.getString("xw_nr"));
					list.add(xinWen);
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public List<JiaoShi> getJiaoShis() {
		List<JiaoShi> list = new ArrayList<JiaoShi>();
		String servlet = "InfoServlet";
		String method = "getJiaoShis";
		String result = HttpUtil.sendHttp(servlet, method, null);
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getXinWens");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					JiaoShi jiaoShi = new JiaoShi();
					jiaoShi.setJs_bh(object.getString("js_bh"));
					jiaoShi.setJs_ly(object.getString("js_ly"));
					jiaoShi.setJs_sy("js_sy");
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public List<String> getJiaoXueLou() {
		List<String> list = new ArrayList<String>();
		String servlet = "InfoServlet";
		String method = "getJiaoXueLou";
		String result = HttpUtil.sendHttp(servlet, method, null);
		result = result.replaceAll("[", "");
		result = result.replaceAll("]", "");
		String[] data = result.split(",");
		if (data.length != 0) {
			for (int i = 0; i < data.length; i++) {
				list.add(data[i]);
			}
			return list;
		}
		return null;
	}

	@Override
	public List<JiaoShi> getJiaoShiByLouYu(String JS_LY) {
		String servlet = "InfoServlet";
		String method = "getJiaoShiByLouYu";
		Map<String, String> map = new HashMap<String, String>();
		map.put("js_ly", JS_LY);
		String result = HttpUtil.sendHttp(servlet, method, map);
		System.out.println(result);
		List<JiaoShi> list = new ArrayList<JiaoShi>();
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getJiaoShiByLouYu");
				for (int i = 0; i < jsonArray.length(); i++) {
					JiaoShi jiaoShi = new JiaoShi();
					JSONObject object = jsonArray.getJSONObject(i);
					jiaoShi.setJs_bh(object.getString("js_bh"));
					jiaoShi.setJs_ly(object.getString("js_ly"));
					jiaoShi.setJs_sy(object.getString("js_sy"));
					list.add(jiaoShi);
				}
				
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	@Override
	public List<String> getJiaoShiByShiyong(String JS_LY) {
		String servlet = "InfoServlet";
		String method = "getJiaoShiByShiyong";
		Map<String, String> map = new HashMap<String, String>();
		map.put("js_ly", JS_LY);
		String result = HttpUtil.sendHttp(servlet, method, map);
		List<String> list = new ArrayList<String>();
		result = result.replaceAll("[", "");
		result = result.replaceAll("]", "");
		String[] data = result.split(",");
		if (data.length != 0) {
			for (int i = 0; i < data.length; i++) {
				list.add(data[i]);
			}
			return list;
		}
		return null;
	}

	@Override
	public List<DianYing> getDianYings() {
		String servlet = "InfoServlet";
		String method = "getDianYings";
		String result = HttpUtil.sendHttp(servlet, method, null);
		List<DianYing> list = new ArrayList<DianYing>();
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getDianYings");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					DianYing dianYing = new DianYing();
					dianYing.setDy_bt(object.getString("dy_bt"));
					dianYing.setDy_dy(object.getString("dy_dy"));
					dianYing.setDy_id(Integer.valueOf(object.getString("dy_id")));
					dianYing.setDy_jj(object.getString("dy_jj"));
					dianYing.setDy_lx(object.getString("dy_lx"));
					dianYing.setDy_sj(object.getString("dy_sj"));
					dianYing.setDy_tp(HttpConstant.URL
							+ object.getString("dy_tp"));
					dianYing.setDy_zy(object.getString("dy_zy"));
					list.add(dianYing);
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<HuoDong> getHuoDongs() {
		return null;
	}

	@Override
	public List<Food> getFoods() {
		List<Food> list = new ArrayList<Food>();
		JSONObject jsonObject=null;
		try {		
			jsonObject = new JSONObject(HttpUtil2.result);
			System.out.println(jsonObject.toString());
			JSONArray jsonArray =jsonObject.getJSONArray("pointList");
			for (int i = 0; i < jsonArray.length(); i++) {			
				JSONObject object = jsonArray.getJSONObject(i);
				Food food = new Food();
				food.setTname(object.getString("name"));
				food.setaddress(object.getString("address"));
				food.setdistrict(object.getString("district"));
				food.setDistance(object.getInt("distance"));
				try {
				String str = object.getString("additionalInformation");
				JSONObject object2=new JSONObject(str);
			    food.settag(object2.getString("tag"));
				food.settelephone(object2.getString("telephone"));
				food.setprice(object2.getString("price"));
				
				try {
					JSONArray jsonArray2 =object2.getJSONArray("link");
				JSONObject object3 = jsonArray2.getJSONObject(0);
				food.setLinkname(object3.getString("name"));
				food.setLinkurl(object3.getString("url"));
				} catch (Exception e) {
					food.setLinkname("0");
					food.setLinkurl("0");
				}
				} catch (Exception e) {
					food.settag("0");
					food.settelephone("0");
					food.setprice("0");
					food.setLinkname("0");
					food.setLinkurl("0");
				}
				food.settype(object.getString("type"));
				list.add(food);
			}	
			return list;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		

	}

	@Override
	public List<ShouCe> getShouCes() {
		String servlet = "InfoServlet";
		String method = "getShenghuoShouCe";
		String result = HttpUtil.sendHttp(servlet, method, null);
		List<ShouCe> list = new ArrayList<ShouCe>();
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getShenghuoShouCe");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					ShouCe shouCe = new ShouCe();
					shouCe.setSh_bt(object.getString("sh_bt"));
					shouCe.setSh_nr(object.getString("sh_nr"));
					shouCe.setSh_id(object.getInt("sh_id"));
					list.add(shouCe);
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<GetInformation> getGetInformations(String xyq_zl) {
		String servlet = "XiaoYuanQiangServlet";
		String method = "getAllXiaoYuanQiangs";
		Map<String, String> map = new HashMap<String, String>();
		map.put("xyq_zl", xyq_zl);
		String result = HttpUtil.sendHttp(servlet, method, map);
		System.out.println(result);
		List<GetInformation> list = new ArrayList<GetInformation>();
		if (!"false".equals(result)) {
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONArray jsonArray = jsonObject.getJSONArray("getAllXiaoYuanQiangs");
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject object = jsonArray.getJSONObject(i);
					GetInformation get = new GetInformation();
					get.setXyq_nr(object.getString("xyq_nr"));
					get.setXyq_sj(object.getString("xyq_sj"));
					get.setXyq_telephone(object.getString("xyq_xs_xh"));
					get.setXyq_xs_xm(object.getString("xyq_xs_xm"));
					get.setXyq_zl(object.getString("xyq_zl"));
					list.add(get);
					System.out.println(get.toString());
				}
				return list;
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return null;
	}


}
